﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Engine.Classes.*;
    import flash.geom.*;
    import flash.utils.*;
    import Classes.*;
    import GameMode.*;

    public class ActionProgressBar extends NPCAction {

        protected static const PRECISION:Number = 0.1;

        protected var m_resource:MapResource;
        protected var m_actionStarted:Boolean = false;
        protected var m_actionTime:Number = 0;
        protected var m_targetTime:Number;
        protected var m_progressResource:MapResource;
        protected var m_actionResource:MapResource;
        protected var m_text:String;
        protected var m_progressStartFunction:Function;
        protected var m_progressEndFunction:Function;
        protected var m_progressCancelFunction:Function;
        protected var m_timeMap:Dictionary;
        protected var m_lastPreciseTime:Number = 0;

        public function ActionProgressBar(_arg1:NPC=null, _arg2:MapResource=null, _arg3:String=null, _arg4:Number=-1){
            this.m_timeMap = new Dictionary(true);
            super(_arg1);
            GMEdit.addLock();
            this.m_resource = _arg2;
            this.m_text = _arg3;
            this.m_targetTime = _arg4;
            if ((((this.m_text == null)) && ((this.m_resource is HarvestableResource)))){
                this.m_text = (this.m_resource as HarvestableResource).getActionText();
            };
            if (this.m_targetTime < 0){
                if ((this.m_resource is HarvestableResource)){
                    this.m_targetTime = (this.m_resource as HarvestableResource).getHarvestTime();
                } else {
                    this.m_targetTime = Global.gameSettings.getNumber("actionBarAny", 2);
                };
            };
            this.m_progressStartFunction = this.m_resource.getProgressBarStartFunction();
            this.m_progressEndFunction = this.m_resource.getProgressBarEndFunction();
            this.m_progressCancelFunction = this.m_resource.getProgressBarCancelFunction();
            if (_arg1){
                this.m_actionResource = _arg1;
            } else {
                this.m_actionResource = _arg2;
            };
            if (((_arg2.deferProgressBarToNPC()) && (_arg1))){
                this.m_progressResource = _arg1;
            } else {
                this.m_progressResource = _arg2;
            };
        }
        public function setTimedCallback(_arg1:Number, _arg2:Function):ActionProgressBar{
            if ((((_arg1 > 0)) && (!((_arg2 == null))))){
                this.m_timeMap[_arg1] = _arg2;
            };
            return (this);
        }
        override public function enter():void{
            super.enter();
            if (this.m_progressStartFunction != null){
                if (!this.m_progressStartFunction()){
                    this.m_actionResource.actionQueue.removeState(this);
                    if (m_npc){
                        Global.world.citySim.pickupManager.clearQueue(m_npc.getItemName());
                    };
                    return;
                };
            };
            this.m_resource.lock();
            this.m_actionStarted = true;
        }
        override public function reenter():void{
            super.reenter();
            if (this.m_progressStartFunction != null){
                if (!this.m_progressStartFunction()){
                    this.m_actionResource.actionQueue.removeState(this);
                    if (m_npc){
                        Global.world.citySim.pickupManager.clearQueue(m_npc.getItemName());
                    };
                    return;
                };
            };
            this.m_resource.lock();
            this.m_actionStarted = true;
        }
        override public function removed():void{
            super.removed();
            GMEdit.removeLock();
            if (!Global.isTransitioningWorld){
                if (!this.m_actionStarted){
                    if (this.m_progressCancelFunction != null){
                        this.m_progressCancelFunction();
                    };
                    return;
                };
                if (this.m_progressEndFunction != null){
                    this.m_progressEndFunction();
                };
            };
            this.m_actionStarted = false;
            this.m_progressResource.setActionProgress(false);
            this.m_progressResource.setActionBarOffset(0, 0);
            this.m_resource.unlock();
        }
        override public function getInterrupt():int{
            return ((this.m_actionStarted) ? State.NO_INTERRUPT : State.NORMAL_INTERRUPT);
        }
        override public function update(_arg1:Number):void{
            var _local2:Number;
            var _local3:Number;
            var _local4:Point;
            if (((this.m_actionStarted) && (!(Global.isTransitioningWorld)))){
                this.m_actionTime = (this.m_actionTime + _arg1);
                _local2 = (this.m_actionTime / this.m_targetTime);
                _local3 = (Number(int((this.m_actionTime / PRECISION))) * PRECISION);
                if ((((this.m_timeMap[_local3] is Function)) && ((_local3 > this.m_lastPreciseTime)))){
                    this.m_lastPreciseTime = _local3;
                    (this.m_timeMap[_local3] as Function)();
                };
                if (_local2 > 1){
                    this.m_actionResource.actionQueue.removeState(this);
                } else {
                    _local4 = this.m_progressResource.getProgressBarOffset();
                    this.m_progressResource.setActionBarOffset(Math.floor(_local4.x), Math.floor(_local4.y));
                    this.m_progressResource.setActionProgress(true, this.m_text, _local2, false);
                };
            };
        }

    }
}//package Classes.actions 
